home *** CD-ROM | disk | FTP | other *** search
/ Java Interactive Reference Guide / Java Interactive Reference Guide.iso / autorun / source.dir / 00110_15.txt < prev    next >
Encoding:
Text File  |  1980-01-11  |  3.9 KB  |  125 lines

  1. /*
  2.  * @(#)SoundArea.java    1.3 95/10/13  
  3.  *
  4.  * Copyright (c) 1994-1995 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL or COMMERCIAL purposes and
  8.  * without fee is hereby granted. 
  9.  * Please refer to the file http://java.sun.com/copy_trademarks.html
  10.  * for further important copyright and trademark information and to
  11.  * http://java.sun.com/licensing.html for further important licensing
  12.  * information for the Java (tm) Technology.
  13.  * 
  14.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  15.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  16.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  17.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  18.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  19.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  20.  * 
  21.  * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
  22.  * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
  23.  * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
  24.  * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
  25.  * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
  26.  * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
  27.  * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES").  SUN
  28.  * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
  29.  * HIGH RISK ACTIVITIES.
  30.  */
  31.  
  32. import java.awt.Graphics;
  33. import java.applet.AudioClip;
  34. import java.net.URL;
  35. import java.net.MalformedURLException;
  36.  
  37. /**
  38.  * An audio feedback ImageArea class.
  39.  * This class extends the basic ImageArea Class to play a sound when
  40.  * the user enters the area.
  41.  *
  42.  * @author     Jim Graham
  43.  * @author     Chuck McManis
  44.  * @version     1.3, 10/13/95
  45.  */
  46. class SoundArea extends ImageMapArea {
  47.     /** The URL of the sound to be played. */
  48.     URL sound;
  49.     AudioClip soundData = null;
  50.     boolean hasPlayed; 
  51.     boolean isReady = false;
  52.     long    lastExit = 0;
  53.     final static int HYSTERESIS = 1500;
  54.  
  55.     /**
  56.      * The argument is the URL of the sound to be played.
  57.      */
  58.     public void handleArg(String arg) {
  59.     try {
  60.         sound = new URL(parent.getDocumentBase(), arg);
  61.     } catch (MalformedURLException e) {
  62.         sound = null;
  63.     }
  64.     hasPlayed = false;
  65.     }
  66.  
  67.     /**
  68.      * The applet thread calls the getMedia() method when the applet
  69.      * is started.
  70.      */
  71.     public void getMedia() {
  72.     if (sound != null) {
  73.         soundData = parent.getAudioClip(sound);
  74.     }
  75.     if (soundData == null) {
  76.         System.out.println("SoundArea: Unable to load data "+sound);
  77.     }
  78.     isReady = true;
  79.     }
  80.  
  81.     /**
  82.      * The enter method is called when the mouse enters the area.
  83.      * The sound is played if the mouse has been outside of the
  84.      * area for more then the delay indicated by HYSTERESIS.
  85.      */
  86.     public boolean enter() {
  87.     // is the sound sample loaded?
  88.     if (! isReady) {
  89.         parent.showStatus("Loading media file...");
  90.         return false;
  91.     }
  92.  
  93.     /*
  94.       * So we entered the selection region, play the sound if
  95.      * we need to. Track the mouse entering and exiting the
  96.      * the selection box. If it doesn't stay out for more than
  97.      * "HYSTERESIS" millis, then don't re-play the sound.
  98.      */
  99.     long now = System.currentTimeMillis();
  100.     if (Math.abs(now - lastExit) < HYSTERESIS) {
  101.         // if within the window pretend that it was played.
  102.         hasPlayed = true;
  103.             return false;
  104.     }
  105.  
  106.     // Else play the sound.
  107.     if (! hasPlayed && (soundData != null)) {
  108.         hasPlayed = true;
  109.         soundData.play();
  110.     }
  111.  
  112.     return false;
  113.     }
  114.  
  115.     /**
  116.      * The exit method is called when the mouse leaves the area.
  117.      */
  118.     public void exit() {
  119.     if (hasPlayed) {
  120.         hasPlayed = false;
  121.         lastExit = System.currentTimeMillis(); // note the time of exit
  122.     }
  123.     }
  124. }
  125.